package com.eqdd.yiqidian.ui.share;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.FriendListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ReflectUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.subscribers.DisposableSubscriber;
import io.rong.imlib.model.Conversation;

public class ShareFriendActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;
    private ArrayList<SelectBean> selectBeans = new ArrayList<>();
    private ArrayList<Object> datas;
    private ShareConf shareConf;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        shareConf = (ShareConf) getIntent().getSerializableExtra("bean");
        if (shareConf == null) {
            shareConf = new ShareConf().setTitle("请选择")
                    .setRightTitle("发送")
                    .setMaxNum(5)
                    .setMaxTip("最多%d");
        }
        initTopTitleBar(View.VISIBLE, shareConf.getTitle());
        initTopRightText(shareConf.getRightTitle(), (i) -> {
            if (selectBeans.size() > 0) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Config.SELECTED_BEANS, selectBeans);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("请进行好友选择");
            }
        });
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_07, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_content, data);
            }
        })
                .register(R.layout.list_item_04, new SlimInjector<SelectBean>() {
                    @Override
                    public void onInject(SelectBean data, IViewInjector injector) {
                        injector.text(R.id.tv_content, data.getContent())
                                .imageCircle(R.id.iv_head, data.getHeadImg())
                                .checked(R.id.checkbox, data.isChecked())
                                .enable(R.id.checkbox, data.isEnable())
                                .text(R.id.tv_right,data.isEnable()?"":shareConf.getEnableTip());
                    }
                }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    SelectBean bean = (SelectBean) slimAdapterEx.getDataItem(position);
                    if (bean.isEnable()) {
                        if (!bean.isChecked() && shareConf.getMaxNum() != -1 && selectBeans.size() >= 5) {
                            ToastUtil.showShort(String.format(shareConf.getMaxTip(), shareConf.getMaxNum()));
                            return;
                        }
                        bean.setChecked(!bean.isChecked());
                        if (bean.isChecked()) {
                            selectBeans.add(bean);
                        } else {
                            selectBeans.remove(bean);
                        }
                        slimAdapterEx.notifyItemChanged(position);
                    }
                });
    }

    private void refresh() {
        datas = new ArrayList<>();
        OkGo.<HttpResult<List<FriendListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_LIST_INFO)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<FriendListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FriendListResultBean>>> response) {
                        Flowable.fromIterable(response.body().getItems())
                                .subscribe(dispo);
                    }
                });
    }


    DisposableSubscriber dispo = new DisposableSubscriber<FriendListResultBean>() {
        @Override
        public void onNext(FriendListResultBean friendListResultBean) {
            SelectBean bean = new SelectBean();
            if (shareConf.getEnabledBeans() != null) {
                for (Object o : shareConf.getEnabledBeans()) {
                    if (ReflectUtil.getField(o, shareConf.getGuidField()).equals(friendListResultBean.getGuid())) {
                        bean.setEnable(false);
                        break;
                    }
                }
            }
            bean.setGuid(friendListResultBean.getGuid());
            bean.setHeadImg(friendListResultBean.getIphoto());
            bean.setContent(friendListResultBean.getUpname());
            bean.setType(Conversation.ConversationType.PRIVATE);
            datas.add(bean);
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

            slimAdapterEx.updateData(datas);
        }
    };

    @Override
    public void setView() {
        refresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        if (dispo != null && !dispo.isDisposed()) {
            dispo.dispose();
        }
        super.onDestroy();
    }
}
